

package com.maoshi.shop.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.bean.app.dto.ProdCommDataDto;
import com.maoshi.shop.bean.app.dto.ProdCommDto;
import com.maoshi.shop.bean.app.param.ProdCommParam;
import com.maoshi.shop.bean.model.OrderItem;
import com.maoshi.shop.bean.model.ProdComm;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.common.util.HttpContextUtils;
import com.maoshi.shop.common.util.IPHelper;
import com.maoshi.shop.dao.OrderItemMapper;
import com.maoshi.shop.dao.ProdCommMapper;
import com.maoshi.shop.service.ProdCommService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 商品评论
 *
 * @author xwc
 * @date 2019-04-19 10:43:57
 */
@Service
@AllArgsConstructor
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService {

    private final ProdCommMapper prodCommMapper;

    private final OrderItemMapper orderItemMapper;

    @Override
    public ProdCommDataDto getProdCommDataByProdId(Long prodId) {
        ProdCommDataDto prodCommDataDto=prodCommMapper.getProdCommDataByProdId(prodId);
        //计算出好评率
        if(prodCommDataDto.getPraiseNumber() == 0||prodCommDataDto.getNumber() == 0){
            prodCommDataDto.setPositiveRating(0.0);
        }else{
//            prodCommDataDto.setPositiveRating(Arith.mul(Arith.div(prodCommDataDto.getPraiseNumber(),prodCommDataDto.getNumber()),100));
            prodCommDataDto.setPositiveRating(Arith.div(Arith.mul(prodCommDataDto.getPraiseNumber(),100),prodCommDataDto.getNumber()));
        }
        return prodCommDataDto;
    }

    @Override
    public IPage<ProdCommDto> getProdCommDtoPageByUserId(Page page, String userId) {
        return prodCommMapper.getProdCommDtoPageByUserId(page,userId);
    }

    @Override
    public IPage<ProdCommDto> getProdCommDtoPageByProdId(Page page, Long prodId, Integer evaluate) {

        IPage<ProdCommDto> prodCommDtos = prodCommMapper.getProdCommDtoPageByProdId(page, prodId, evaluate);
        prodCommDtos.getRecords().forEach(prodCommDto -> {
            // 匿名评价
            if (prodCommDto.getIsAnonymous() == 1) {
                prodCommDto.setNickName(null);
                prodCommDto.setPic(null);
            }
        });
        return prodCommDtos;
    }

    @Override
    public IPage<ProdComm> getProdCommPage(Page page,ProdComm prodComm,Long shopId) {
        return prodCommMapper.getProdCommPage(page,prodComm,shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comm(OrderItem orderItem, ProdCommParam prodCommParam) {
        // 插入评论
        ProdComm prodComm = new ProdComm();
        prodComm.setProdId(orderItem.getProdId());
        prodComm.setOrderItemId(orderItem.getOrderItemId());
        prodComm.setUserId(orderItem.getUserId());
        prodComm.setScore(prodCommParam.getScore());
        prodComm.setContent(prodCommParam.getContent());
        prodComm.setPics(prodCommParam.getPics());
        prodComm.setIsAnonymous(prodCommParam.getIsAnonymous());
        prodComm.setRecTime(new Date());
        // 平台不需要控评
        prodComm.setStatus(1);
        prodComm.setPostip(IPHelper.getIpAddr());
        // 好评
        if (Objects.equals(prodComm.getScore(), 5) || Objects.equals(prodComm.getScore(), 4)) {
            prodComm.setEvaluate(0);
        }
        // 中评
        else if(Objects.equals(prodComm.getScore(), 3)) {
            prodComm.setEvaluate(1);
        }
        // 差评
        else if(Objects.equals(prodComm.getScore(), 1) || Objects.equals(prodComm.getScore(), 2)) {
            prodComm.setEvaluate(2);
        }
        prodCommMapper.insert(prodComm);

        // 更新订单项评论状态
        orderItem.setCommSts(1);
        orderItemMapper.updateById(orderItem);
    }
}
